{% extends 'base.html' %}
{% load static %}

{% block title %}
    <title>业务线编辑</title>
{% endblock title %}

{% block navheader %}
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>业务线编辑</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="{% url 'assets:dashboard' %}">首页</a></li>
                        <li class="breadcrumb-item"><a href="{% url 'assets:business_unit_list' %}">主机列表</a></li>
                        <li class="breadcrumb-item active">主机编辑</li>
                    </ol>
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>
{% endblock navheader %}

{% block content %}
    <div class="card">
        <div class="card-header">
            <h3 class="card-title">主机编辑</h3>
        </div>
        <!-- /.card-header -->
        <div class="card-body row">
            <input type="text" id="busid" value="{{ business.id }}" hidden>
            <div class="col-2 pt-1 pb-1">业务线名称<span style="color:red;"> * </span>：</div>
            <div class="col-10 pt-1 pb-1"><input class="form-control" type="text" id="name"
                                                 value="{{ business.name }}" disabled></div>
            <div class="col-2 pt-1 pb-1">备注：</div>
            <div class="col-10 pt-1 pb-1"><textarea id="memo"
                                                    class="form-control">{{ business.memo | default:'' }}</textarea>
            </div>
            <div class="offset-2 col-10 pt-2">
                <a class="btn btn-default" href="{% url 'assets:business_unit_list' %}">返回</a>
                <button class="btn btn-success ml-2" onclick="changebusiness(this);">提交</button>
            </div>
        </div>
        <!-- /.card-body -->
    </div>
    <!-- /.card -->
{% endblock content %}

{% block js %}
   <script>
        // 修改业务线信息
        changebusiness = function (event) {
            toastr.options.closeButton = true;
            toastr.options.showMethod = 'slideDown';
            toastr.options.hideMethod = 'fadeOut';
            toastr.options.closeMethod = 'fadeOut';
            toastr.options.timeOut = 3000;
            toastr.options.extendedTimeOut = 0;
            toastr.options.progressBar = true;
            toastr.options.positionClass = 'toast-top-right';

            $(event).removeAttr("onclick");
            $(event).attr("disabled", true);

            var busid = $('#busid').val();
            var name = $('#name').val();
            var memo = $('#memo').val();

            csrfmiddlewaretoken = '{{ request.COOKIES.csrftoken }}';

            $.ajax({
                url: "{% url 'assets_api:business_update' %}",
                async: true,
                type: 'POST',
                dataType: 'json',
                data: {
                    'csrfmiddlewaretoken': csrfmiddlewaretoken,
                    'busid': busid,
                    'name': name,
                    'memo': memo,
                },
                timeout: 5000,
                cache: true,
                beforeSend: LoadFunction, //加载执行方法
                error: errFunction,  //错误执行方法
                success: succFunction, //成功执行方法
            });

            function LoadFunction() {
                // 提交中
            };

            function errFunction() {
                // 消息框
                toastr.error('更新业务线错误');
                $(event).removeAttr("disabled");
                $(event).attr("onclick", "changebusiness(this);");
            };

            function succFunction(res) {
                if (res.code != 200) {
                    // 消息框
                    toastr.error('更新业务线错误: ' + res.err);
                    $(event).removeAttr("disabled");
                    $(event).attr("onclick", "changebusiness(this);");
                } else {
                    // 消息框
                    toastr.options.timeOut = 1000;
                    toastr.options.onHidden = function () {
                        window.location.href = "{% url 'assets:business_detail' business.id %}"; // 跳转到详细页面
                    }
                    toastr.success('更新业务线成功');
                }
            };
        }
    </script>
{% endblock js %}